package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.HigherStatus;
import com.ruoyi.common.enums.TenantStatus;
import com.ruoyi.common.enums.UnitStatus;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysExpense;
import com.ruoyi.system.domain.SysHigher;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.domain.SysUnit;
import com.ruoyi.system.service.ISysBuildingService;
import com.ruoyi.system.service.ISysHigherService;
import com.ruoyi.system.service.ISysTenantService;
import com.ruoyi.system.service.ISysUnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 涨租Controller
 *
 * @author ruoyi
 * @date 2020-06-28
 */
@Controller
@RequestMapping("/system/higher")
public class SysHigherController extends BaseController {
    private String prefix = "system/higher";

    @Autowired
    private ISysTenantService sysTenantService;

    @Autowired
    private ISysHigherService sysHigherService;

    @RequiresPermissions("system:higher:view")
    @GetMapping()
    public String higher() {
        return prefix + "/higher";
    }

    /**
     * 查询涨租列表
     */
    @RequiresPermissions("system:higher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTenant sysTenant) {
        startPage();
        sysTenant.setRisePriceStatus(HigherStatus.WAIT.getCode());
        sysTenant.setTenantStatus(TenantStatus.LIVEING.getCode());
        List<SysTenant> list = sysTenantService.selectSysTenantHigherList(sysTenant);
        for(SysTenant listTwo:list){
            sysTenantService.exchange(listTwo);
        }
        return getDataTable(list);
    }

    /**
     * 确认涨租
     */
    @RequiresPermissions("system:higher:confirm")
    @Log(title = "确认涨租", businessType = BusinessType.INSERT)
    @PostMapping("/confirm")
    @ResponseBody
    public AjaxResult confirm(SysTenant sysTenant) {

        SysTenant realTenant = sysTenantService.selectSysTenantById(sysTenant.getTenantId());
        // 原先涨租时间
        Date originRisePriceFirstTime = realTenant.getRisePriceFirstTime();
        Calendar cal = Calendar.getInstance();
        cal.setTime(originRisePriceFirstTime);
        cal.add(Calendar.YEAR, 1);//增加一年
        // 涨租一年的时间
        Date newRisePriceTime = cal.getTime();
        realTenant.setNextHigherTime(newRisePriceTime);
        // 更新
        sysTenantService.updateSysTenant(realTenant);


        SysHigher sysHigher = new SysHigher();
        sysHigher.setTenantId(sysTenant.getTenantId());

        Double higherRentFee = Double.parseDouble(sysTenant.getMonthlyRentFee()) * (1 + Double.parseDouble(sysTenant.getRisePriceRatio()));
        sysHigher.setHigherRentFee(higherRentFee);
        sysHigher.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();

            return toAjax(sysHigherService.insertSysHigher(sysHigher));
    }

    /**
     * 涨租详情
     */
    @RequiresPermissions("system:higher:detail")
    @GetMapping("/detail/{tenantId}")
    public String detail(@PathVariable("tenantId") Long tenantId, ModelMap mmap) {
        SysTenant sysTenant = sysTenantService.selectSysTenantById(tenantId);
        Double higherRentFee = Double.parseDouble(sysTenant.getMonthlyRentFee()) * (1 + Double.parseDouble(sysTenant.getRisePriceRatio()));
        higherRentFee = DoubleUtil.round(higherRentFee,2);
        mmap.put("higherRentFee", higherRentFee);
        Double risePriceRatio = Double.parseDouble(sysTenant.getRisePriceRatio()) * 100;
        sysTenant.setRisePriceRatio(risePriceRatio.toString());
        sysTenant.setMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.valueOf(sysTenant.getMonthlyRentFee()),2)));
        mmap.put("syaTenant", sysTenant);
        return prefix + "/detail";
    }

    /**
     * 导出涨租列表
     */
    @RequiresPermissions("system:higher:export")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTenant sysTenant) {
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        return util.exportExcel(list, "higher");
    }

}
